local Screen = require "widgets/screen"
local TEMPLATES = require "widgets/redux/templates"
local LabelTextBox = require ("core_ShadowFieldExciter/widgets/LabelTextBox")
local PopupDialogScreen = require "core_ShadowFieldExciter/screens/PopupDialogScreen"
local Image = require "widgets/image"
local ImageButton = require "widgets/imagebutton"
local Text = require "widgets/text"
local db = TUNING.MOD_ShadowFieldExciter
local strings = STRINGS.MOD_ShadowFieldExciter

local label_width = 200 -- width of the label on the wide fields
local input_width = 500
local field_nudge = -55
local label_height = 40
local space_between = 5
local font_size = 25

local PRESET_NAME_MAX_LENGTH = strings.Preset_Name_Max_Length
local PRESET_DESCRIPTION_MAX_LENGTH = strings.Preset_Description_Max_Length

local INVALID_CHARACTER_FILTER = [[<>"/\|?*]]
local invalidcharints = {}
for i = 1, 31 do table.insert(invalidcharints, i) end
INVALID_CHARACTER_FILTER = INVALID_CHARACTER_FILTER..string.char(unpack(invalidcharints))


local NamePresetScreen = Class(Screen, function(self, title, confirmstr, onconfirmfn, editingpresetid, name, desc)
    assert(onconfirmfn, "NamePresetScreen requires a onconfirmfn")

    Screen._ctor(self, "NamePresetScreen")

    self.onconfirmfn = onconfirmfn
    self.editingpresetid = editingpresetid

    self.root = self:AddChild(TEMPLATES.ScreenRoot())
    self.bg = self.root:AddChild(TEMPLATES.BackgroundTint(0.7))

    --#region 窗口背景
    self.window = self.root:AddChild(Image("images/my_futuristic_ui/my_futuristic_ui.xml", "name_preset_bg.tex"))
    self.window:SetScale(0.9, 0.9)
    --#endregion

    --#region 窗口标题
    self.title = self.window:AddChild(Text(NEWFONT_OUTLINE, 50, title or STRINGS.UI.CUSTOMIZATIONSCREEN.PRESET_TITLE, {1, 1, 1, 1}))
    self.title:SetPosition(0, 110)
    self.title:SetHAlign(ANCHOR_MIDDLE)
    self.title:SetVAlign(ANCHOR_MIDDLE)
    --#endregion

    --#region 预设名称输入框
    self.preset_name = self.window:AddChild(LabelTextBox(strings.Preset_Name, name or "", label_height, space_between,field_nudge,
    {
        width = label_width,
        font = NEWFONT_OUTLINE,
        font_size = font_size,
        color = {1, 1, 1, 1}
    }, 
    {
        width = input_width,
        font = NEWFONT,
        font_size = font_size,
        atlas = "images/my_futuristic_ui/my_futuristic_ui.xml",
        normal = "textbox_normal.tex",
        hover = "textbox_hover.tex",
        focus = "textbox_focus.tex",
        color = {0, 0, 0, 1}
    }))
    self.preset_name:SetScale(1.2, 1.2)
    self.preset_name.textbox:SetTextLengthLimit(PRESET_NAME_MAX_LENGTH)
    self.preset_name.textbox:SetInvalidCharacterFilter(INVALID_CHARACTER_FILTER)
    self.preset_name:SetPosition(0, 50)
    --#endregion

    --#region 预设描述输入框
    self.preset_desc = self.window:AddChild(LabelTextBox(strings.Preset_Desc, desc or "", label_height, space_between, field_nudge,
    {
        width = label_width,
        font = NEWFONT_OUTLINE,
        font_size = font_size,
        color = {1, 1, 1, 1}
    }, {
        width = input_width,
        font = NEWFONT,
        font_size = font_size,
        atlas = "images/my_futuristic_ui/my_futuristic_ui.xml",
        normal = "textbox_normal.tex",
        hover = "textbox_hover.tex",
        focus = "textbox_focus.tex",
        color = {0, 0, 0, 1} -- Default color black
    }))
    self.preset_desc:SetScale(1.2, 1.2)
    self.preset_desc.textbox:SetTextLengthLimit(PRESET_DESCRIPTION_MAX_LENGTH)
    self.preset_desc:SetPosition(0, -20)
    --#endregion
    
    --#region 取消按钮
    self.btn_cancel = self.window:AddChild(ImageButton(
        "images/my_futuristic_ui/my_futuristic_ui.xml",
        "preset_button_normal.tex",
        "preset_button_hover.tex",
        "preset_button_disabled.tex",
        "preset_button_down.tex"
    ))
    self.btn_cancel:SetPosition(-200, -100)
    self.btn_cancel:SetScale(0.8, 0.4)
    self.btn_cancel:SetOnClick(function() self:Close() end)
    self.btn_cancel:SetText(STRINGS.UI.CUSTOMIZATIONSCREEN.BACK)
    self.btn_cancel:SetFont(NEWFONT_OUTLINE)
    self.btn_cancel:SetTextColour(1, 1, 1, 1)
    self.btn_cancel:SetTextFocusColour(1, 1, 1, 1)
    self.btn_cancel:SetTextSelectedColour(1, 1, 1, 1)
    self.btn_cancel.text:SetScale(1.25, 2.5)
    self.btn_cancel.text:SetAutoSizingString(STRINGS.UI.CUSTOMIZATIONSCREEN.BACK,self.btn_cancel:GetSize()*0.7)
    --#endregion

    --#region 确认按钮
    self.btn_confirm = self.window:AddChild(ImageButton(
        "images/my_futuristic_ui/my_futuristic_ui.xml",
        "preset_button_normal.tex",
        "preset_button_hover.tex",
        "preset_button_disabled.tex",
        "preset_button_down.tex"
    ))

    self.btn_confirm:SetPosition(200, -100)
    self.btn_confirm:SetScale(0.8, 0.4)
    self.btn_confirm:SetOnClick(function() self:SavePreset() end)
    self.btn_confirm:SetText(confirmstr)
    self.btn_confirm:SetFont(NEWFONT_OUTLINE)
    self.btn_confirm:SetTextColour(1, 1, 1, 1)
    self.btn_confirm:SetTextFocusColour(1, 1, 1, 1)
    self.btn_confirm:SetTextSelectedColour(1, 1, 1, 1)
    self.btn_confirm.text:SetScale(1.25, 2.5)
    self.btn_confirm.text:SetAutoSizingString(confirmstr,self.btn_confirm:GetSize()*0.7)
    --#endregion

    self.default_focus = self.preset_name

    self:DoFocusHookups()
end)

function NamePresetScreen:SavePreset()
    local name = self.preset_name.textbox:GetString()
    local desc = self.preset_desc.textbox:GetString()

    if not name or #name:gsub("%s", "") == 0 then
        TheFrontEnd:PushScreen(
            PopupDialogScreen(STRINGS.UI.CUSTOMIZATIONSCREEN.MISSINGPRESETNAME_TITLE, STRINGS.UI.CUSTOMIZATIONSCREEN.MISSINGPRESETNAME_BODY,
            {},
            1,
            {
                cancel = {
                    scale = {0.8, 0.4},
                    text_scale = {1.25, 2.5},
                    text = STRINGS.UI.CUSTOMIZATIONSCREEN.BACK,
                    cb = function()
                        --TheFrontEnd:PopScreen()
                    end,
                }
            })
        )
        return
    end

    --  检查预设名称是否重复
    if db.PresetData.list[name] then
        TheFrontEnd:PushScreen(
            PopupDialogScreen(STRINGS.UI.CUSTOMIZATIONSCREEN.PRESETEXISTS_TITLE,string.format(strings.PresetExists, name),
            {},
            2,
            {
                cancel = {
                    scale = {0.8, 0.4},
                    text_scale = {1.25, 2.5},
                    text = STRINGS.UI.CUSTOMIZATIONSCREEN.BACK,
                    cb = function()
                        --TheFrontEnd:PopScreen()
                    end,
                },
                confirm = {
                    scale = {0.8, 0.4},
                    text_scale = {1.25, 2.5},
                    text = STRINGS.UI.CUSTOMIZATIONSCREEN.OVERWRITE,
                    cb = function()
                        self:Close()
                        self.onconfirmfn(name, desc)
                    end,
                }
            })
        )
        return
    end


    self:Close()
    self.onconfirmfn(name, desc)
end

function NamePresetScreen:Close()
    TheFrontEnd:PopScreen()
end


function NamePresetScreen:DoFocusHookups()
    self.preset_name:SetFocusChangeDir(MOVE_DOWN, self.preset_desc.textbox)

    self.preset_desc:SetFocusChangeDir(MOVE_UP, self.preset_name.textbox)
    self.preset_desc:SetFocusChangeDir(MOVE_DOWN, self.window)

    self.window:SetFocusChangeDir(MOVE_UP, self.preset_desc.textbox)
end

function NamePresetScreen:OnControl(control, down)
    if NamePresetScreen._base.OnControl(self, control, down) then return true end

    if not down then
        if control == CONTROL_CANCEL then
            self:Close()
            return true
        end
    end
end

return NamePresetScreen